package com.uberspot.project3;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class HistoryActivity extends AppCompatActivity {
    private LinearLayout historyList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        historyList = findViewById(R.id.historyList);

        // 获取历史记录
        SharedPreferences prefs = getSharedPreferences("game", MODE_PRIVATE);
        String history = prefs.getString("history", "No history available");

        // 显示历史记录
        if (!history.equals("No history available")) {
            String[] scores = history.split(",");
            for (String score : scores) {
                TextView scoreView = new TextView(this);
                scoreView.setText(score.trim()); // 去除可能的前后空格
                scoreView.setPadding(8, 8, 8, 8);
                scoreView.setBackgroundColor(Color.LTGRAY); // 设置背景颜色
                scoreView.setTextColor(Color.BLACK); // 设置文字颜色
                scoreView.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                historyList.addView(scoreView);
            }
        } else {
            TextView noHistoryView = new TextView(this);
            noHistoryView.setText("No history available");
            noHistoryView.setPadding(8, 8, 8, 8);
            noHistoryView.setBackgroundColor(Color.LTGRAY);
            noHistoryView.setTextColor(Color.BLACK);
            noHistoryView.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            historyList.addView(noHistoryView);
        }

        // 设置返回按钮的点击事件
        Button backButton = findViewById(R.id.backButton);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 关闭当前Activity，返回主界面
            }
        });
    }
}